package com.test.util;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class BatchDownTextToSpeech {


    private static String textToSpeech(String text) {

        HttpResponse execute = HttpUtil.createPost("https://www.text-to-speech.cn/getSpeek.php")
                .header("Origin", "https://www.text-to-speech.cn")
                .header("Referer", "https://www.text-to-speech.cn/")
                .header("Sec-Ch-Ua", "\"Microsoft Edge\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"")
                .header("Sec-Ch-Ua-Mobile", "?0")
                .header("Sec-Ch-Ua-Platform", "\"Windows\"")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-origin")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0")
                .header("X-Requested-With", "XMLHttpRequest")
                .form("language", "中文（普通话，简体）")
                .form("voice", "zh-CN-XiaoxiaoNeural")
                .form("text", text)
                .form("role", 0)
                .form("style", 0)
                .form("rate", 0)
                .form("pitch", 0)
                .form("kbitrate", "audio-16khz-32kbitrate-mono-mp3")
                .form("silence", "")
                .form("styledegree", 1)
                .form("volume", 75)
                .form("predict", 0)
                .form("user_id", "")
                .form("yzm", "")
                .form("replice", 1)
                .execute();
        return execute.body();


    }

    public static void main(String[] args) {
        String textToSpeech = textToSpeech("第一俄国");
        System.out.println(textToSpeech);
    }

    private void urlTOFile(String url, String fileName) {
        try {
            URL url1 = new URL(url);
            URLConnection conn = url1.openConnection();
            InputStream in = conn.getInputStream();
            FileOutputStream out = new FileOutputStream(fileName);
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
